<template>
	<view class="address-edit">
		<view class="header">
			<text class="title">{{ isEdit ? '编辑地址' : '新增地址' }}</text>
		</view>
		
		<view class="form">
			<view class="form-item">
				<text class="label">收货人</text>
				<input 
					v-model="formData.name" 
					type="text" 
					placeholder="请输入收货人姓名"
					class="input"
				/>
			</view>
			
			<view class="form-item">
				<text class="label">手机号码</text>
				<input 
					v-model="formData.phone" 
					type="number" 
					placeholder="请输入手机号码"
					class="input"
				/>
			</view>
			
			<view class="form-item">
				<text class="label">所在地区</text>
				<view class="region-select" @click="showRegionPicker">
					<text class="region-text" :class="{ placeholder: !formData.province }">
						{{ formData.province ? `${formData.province} ${formData.city} ${formData.district}` : '请选择省市区' }}
					</text>
					<text class="arrow">›</text>
				</view>
			</view>
			
			<view class="form-item">
				<text class="label">详细地址</text>
				<textarea 
					v-model="formData.detail" 
					placeholder="请输入详细地址（街道、门牌号等）"
					class="textarea"
					maxlength="100"
				></textarea>
			</view>
			
			<view class="form-item">
				<label class="checkbox-label">
					<checkbox-group @change="toggleDefault">
						<checkbox :checked="formData.isDefault" />
					</checkbox-group>
					<text class="checkbox-text">设为默认地址</text>
				</label>
			</view>
		</view>
		
		<view class="bottom-actions">
			<button class="save-btn" @click="saveAddress">保存地址</button>
		</view>
		
		<!-- 地区选择器 -->
		<uni-data-picker 
			v-if="showPicker"
			:localdata="regionData"
			@change="onRegionChange"
			@nodeclick="onNodeClick"
			@hide="hideRegionPicker"
		/>
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'

const isEdit = ref(false)
const showPicker = ref(false)

const formData = ref({
	id: null,
	name: '',
	phone: '',
	province: '',
	city: '',
	district: '',
	detail: '',
	isDefault: false
})

// 模拟地区数据
const regionData = ref([
	{
		text: '北京市',
		value: '110000',
		children: [
			{
				text: '北京市',
				value: '110100',
				children: [
					{ text: '东城区', value: '110101' },
					{ text: '西城区', value: '110102' },
					{ text: '朝阳区', value: '110105' },
					{ text: '丰台区', value: '110106' },
					{ text: '海淀区', value: '110108' }
				]
			}
		]
	},
	{
		text: '上海市',
		value: '310000',
		children: [
			{
				text: '上海市',
				value: '310100',
				children: [
					{ text: '黄浦区', value: '310101' },
					{ text: '徐汇区', value: '310104' },
					{ text: '长宁区', value: '310105' },
					{ text: '静安区', value: '310106' },
					{ text: '浦东新区', value: '310115' }
				]
			}
		]
	},
	{
		text: '广东省',
		value: '440000',
		children: [
			{
				text: '广州市',
				value: '440100',
				children: [
					{ text: '荔湾区', value: '440103' },
					{ text: '越秀区', value: '440104' },
					{ text: '海珠区', value: '440105' },
					{ text: '天河区', value: '440106' },
					{ text: '白云区', value: '440111' }
				]
			},
			{
				text: '深圳市',
				value: '440300',
				children: [
					{ text: '罗湖区', value: '440303' },
					{ text: '福田区', value: '440304' },
					{ text: '南山区', value: '440305' },
					{ text: '宝安区', value: '440306' },
					{ text: '龙岗区', value: '440307' }
				]
			}
		]
	}
])

onLoad((options) => {
	if (options.id) {
		isEdit.value = true
		// 模拟编辑数据
		const editData = {
			id: 1,
			name: '张三',
			phone: '13888888888',
			province: '北京市',
			city: '北京市',
			district: '朝阳区',
			detail: '建国路88号SOHO现代城',
			isDefault: true
		}
		formData.value = { ...editData }
	}
})

const showRegionPicker = () => {
	showPicker.value = true
}

const hideRegionPicker = () => {
	showPicker.value = false
}

const onRegionChange = (e) => {
	const { value } = e.detail
	if (value.length >= 3) {
		formData.value.province = value[0].text
		formData.value.city = value[1].text
		formData.value.district = value[2].text
		hideRegionPicker()
	}
}

const onNodeClick = (node) => {
	// 节点点击事件
}

const toggleDefault = () => {
	formData.value.isDefault = !formData.value.isDefault
}

const saveAddress = () => {
	if (!formData.value.name) {
		uni.showToast({
			title: '请输入收货人姓名',
			icon: 'none'
		})
		return
	}
	
	if (!formData.value.phone) {
		uni.showToast({
			title: '请输入手机号码',
			icon: 'none'
		})
		return
	}
	
	if (!formData.value.province) {
		uni.showToast({
			title: '请选择所在地区',
			icon: 'none'
		})
		return
	}
	
	if (!formData.value.detail) {
		uni.showToast({
			title: '请输入详细地址',
			icon: 'none'
		})
		return
	}
	
	uni.showToast({
		title: isEdit.value ? '地址修改成功' : '地址添加成功',
		icon: 'success'
	})
	
	setTimeout(() => {
		uni.navigateBack()
	}, 1500)
}
</script>

<style scoped>
.address-edit {
	background: #f5f5f5;
	min-height: 100vh;
	padding: 20rpx;
}

.header {
	text-align: center;
	padding: 30rpx 0;
}

.title {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
}

.form {
	background: white;
	border-radius: 16rpx;
	padding: 0 30rpx;
	margin-bottom: 30rpx;
}

.form-item {
	display: flex;
	flex-direction: column;
	padding: 30rpx 0;
	border-bottom: 1rpx solid #f0f0f0;
}

.form-item:last-child {
	border-bottom: none;
}

.label {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 20rpx;
	font-weight: bold;
}

.input,
.textarea {
	font-size: 28rpx;
	color: #333;
	background: #f8f9fa;
	border: 2rpx solid #eee;
	border-radius: 8rpx;
	padding: 20rpx;
}

.textarea {
	height: 120rpx;
	resize: none;
}

.region-select {
	display: flex;
	align-items: center;
	justify-content: space-between;
	background: #f8f9fa;
	border: 2rpx solid #eee;
	border-radius: 8rpx;
	padding: 20rpx;
}

.region-text {
	font-size: 28rpx;
	color: #333;
}

.region-text.placeholder {
	color: #999;
}

.arrow {
	font-size: 32rpx;
	color: #999;
}

.checkbox-label {
	display: flex;
	align-items: center;
	gap: 15rpx;
}

.checkbox-text {
	font-size: 26rpx;
	color: #333;
}

.bottom-actions {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background: white;
	padding: 25rpx 30rpx;
	border-top: 1rpx solid #eee;
}

.save-btn {
	background: #ff6b6b;
	color: white;
	border: none;
	border-radius: 25rpx;
	padding: 25rpx;
	font-size: 28rpx;
	font-weight: bold;
	width: 100%;
}
</style>